<?php
/**
 * Description of Fsm
 *
 * @author chedim
 */
class oFsm extends oClass
{
    /**
     * state of machine
     * @var oFsmState 
     */
    private $state  = null;
    private $mode   = 0;

    /**
     *
     * @var oInterfaceReader
     */
    private $source = null;

    public function  __construct(oInterfaceFsmState $start_state)
    {
        $this->state = $start_state;
    }

    public function ReadFile(oFile $source, $mode)
    {
        throw new eNotImplemented();
    }

    public function ReadString(String $source)
    {
        $this->source = new oStringReader($source);
        $this->StartMainLoop();
    }

    protected function StartMainLoop() {
        try {
            while ($element = $this->NextElement()) {
                $this->state = $this->state->PushElement($element);
            }
        } catch (eNotReadable $e) {
            return;
        }
    }

    protected function NextElement() {
        switch ($this->mode) {
        case oFsmMode::CHARACTER:
            return $this->source->Read(1);
            break;

        case oFsmMode::LINE:
            return $this->source->ReadTo("\n");
            break;
        case oFsmMode::LINE_TRIMMED:
            return trim($this->source->read_to("\n"));
            break;
        default:
            throw new eInvalidFsmMode();
        }
    }
}
?>
